package com.futurelight.recipes.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.futurelight.recipes.R;
import com.futurelight.recipes.adapter.DetailAdapter;
import com.futurelight.recipes.dao.EntityFavorite;
import com.futurelight.recipes.model.FavoriteViewModel;
import com.futurelight.recipes.model.Result;
import com.google.android.material.chip.Chip;

public class DetailActivity extends AppCompatActivity {

    private Result result;
    private int width;
    private View detail_btn;
    private View ingredient;
    private Chip scrollableShip;
    private ViewPager2 viewPager2;
    private ImageView start;
    private ImageView imageView;
    private TextView title;
    private TextView timer;
    private View rootView;
    private FavoriteViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        getSupportActionBar().hide();

        result = (Result) getIntent().getExtras().getSerializable("result");
        width = getWindowManager().getDefaultDisplay().getWidth();
        viewModel = new ViewModelProvider(this,
                (ViewModelProvider.Factory) ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()))
                .get(FavoriteViewModel.class);
        initView();
        initListener();
    }

    private void initView() {
        rootView = findViewById(R.id.detail_activity);
        start = findViewById(R.id.start);
        imageView = rootView.findViewById(R.id.detail_image);
        title = findViewById(R.id.detail_title);
        timer = findViewById(R.id.cook_min);
        title.setText(result.getTitle());
        timer.setText(String.valueOf(result.readyInMinutes));
        Glide.with(this).load(result.getImage()).error(R.drawable.plate).into(imageView);

        viewPager2 = findViewById(R.id.detail_view_pager);

        DetailAdapter detailAdapter = new DetailAdapter(getSupportFragmentManager(), getLifecycle());
        detailAdapter.setResult(result);

        detail_btn = findViewById(R.id.detail_btn);
        ingredient = findViewById(R.id.ingredients_btn);
        scrollableShip = findViewById(R.id.scrollable_chip);
        viewPager2.setAdapter(detailAdapter);

    }

    private void initPagerChange() {
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                viewPager2.setCurrentItem(position);
                if (position == 0) {
                    detail_btn.callOnClick();
                } else {
                    ingredient.callOnClick();
                }
            }
        });
    }

    private void initListener() {
        selectDetail();
        selectIngredient();
        initPagerChange();
        initStart();
    }
    /**
     * 初始化喜欢按钮设置监听器，并绑定ViewModel
     */
    private void initStart() {

        viewModel.isFavorite().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                start.setSelected(aBoolean);
            }
        });
        start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.isSelected()) {
                    v.setSelected(false);
                    viewModel.deleteFavorite(new EntityFavorite(result.id));
                } else {
                    v.setSelected(true);
                    viewModel.insertFavorite(new EntityFavorite(result.id));
                }
            }
        });
        viewModel.isFavorite(result.id);
    }

    /**
     * Detail按钮监听器
     */
    void selectDetail() {
        detail_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!detail_btn.isSelected()) {
                    detail_btn.setSelected(true);
                    ingredient.setSelected(false);
                    viewPager2.setCurrentItem(0);
                    scrollableShip.animate()
                            .setDuration(300)
                            .translationX(0)
                            .start();
                }
            }
        });

    }

    /**
     * Inggredient按钮监听器
     */
    void selectIngredient() {
        ingredient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ingredient.isSelected()) {
                    ingredient.setSelected(true);
                    detail_btn.setSelected(false);
                    viewPager2.setCurrentItem(1);
                    scrollableShip.animate()
                            .setDuration(300)
                            .translationX(width / 2)
                            .start();
                }
            }
        });
    }


    /**
     * 详情页结束返回动画
     */
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_pop_enter, R.anim.anim_pop_exit);
    }
}